package com.zx.sdk.league.member;

import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.alliance.ssp.ad.api.BaseAllianceAd;
import com.alliance.ssp.ad.api.SAAllianceAd;
import com.alliance.ssp.ad.api.SAAllianceAdInitParams;
import com.alliance.ssp.ad.api.SAAllianceAdParams;
import com.alliance.ssp.ad.api.SAAllianceAdSdk;
import com.alliance.ssp.ad.api.interstitial.SAInterstitialAd;
import com.alliance.ssp.ad.api.interstitial.SAInterstitialAdInteractionListener;
import com.alliance.ssp.ad.api.interstitial.SAInterstitialAdLoadListener;
import com.alliance.ssp.ad.api.reward.SARewardVideoAd;
import com.alliance.ssp.ad.api.reward.SARewardVideoAdInteractionListener;
import com.alliance.ssp.ad.api.reward.SARewardVideoAdLoadListener;
import com.alliance.ssp.ad.api.splash.SASplashAd;
import com.alliance.ssp.ad.api.splash.SASplashAdInteractionListener;
import com.alliance.ssp.ad.api.splash.SASplashAdLoadListener;
import com.zx.sdk.ZxSDK;
import com.zx.sdk.listener.BaseZxRewardListener;
import com.zx.sdk.listener.BaseZxSplashListener;
import com.zx.sdk.model.AdInfo;
import com.zx.sdk.model.ReadyAdPosition;
import com.zx.sdk.model.ZxError;

import java.util.HashMap;

public class YouTui extends BaseLeagueMember<SAAllianceAd, SAAllianceAd, Object, SAAllianceAd, Object> {

    @Override
    public String getName() {
        return "yt";
    }

    @Override
    protected void onInit(Context context, String appId, boolean initialized) {
        if (!initialized) {
            SAAllianceAdSdk.init(appId, context.getApplicationContext(), new SAAllianceAdInitParams.Builder()
                    .setDebug(ZxSDK.isDebug())
                    .build());
        }
    }

    @Override
    protected void onLoadSplash(AdInfo adInfo, ViewGroup container, String mode, String type, BaseZxSplashListener listener) {
        cpmCache.put(ZxSDK.SPLASH + adInfo.getMapPid(), -1);
        SAAllianceAdParams params = new SAAllianceAdParams();
        params.setPosId(adInfo.getMapPid());
        Context activity = container.getContext();
        if (!(activity instanceof Activity)) return;
        SAAllianceAd saAllianceAd = SAAllianceAdSdk.getSAAllianceAdManager().createSAAllianceAd((Activity) activity);
        saAllianceAd.loadSASplashAd(params, container, 3000, new SASplashAdLoadListener() {
            @Override
            public void onTimeOut() {
                switch (mode) {
                    case ZxSDK.FETCH_ONLY ->
                            listener.onPreLoadNoAD(YouTui.this, new ZxError("-1", "time out"), adInfo);
                    case ZxSDK.FETCH_AND_SHOW ->
                            listener.onNoAD(YouTui.this, new ZxError("-1", "time out"), adInfo);
                }
            }

            @Override
            public void onSplashAdLoad(SASplashAd ad) {
                if (ad == null) {
                    return;
                }
                cpmCache.put(ZxSDK.SPLASH + adInfo.getMapPid(), getEcpm(ad));
                ad.setSplashAdInteractionListener(new SASplashAdInteractionListener() {
                    @Override
                    public void onAdClick() {
                        listener.onADClicked(YouTui.this, adInfo);
                    }

                    @Override
                    public void onAdShow() {
                        listener.onADExposure(YouTui.this, adInfo);
                    }

                    @Override
                    public void onAdTimeOver() {
                        listener.onADDismissed(YouTui.this, adInfo);
                    }

                    @Override
                    public void onAdSkip() {
                        listener.onADDismissed(YouTui.this, adInfo);
                    }
                });
                switch (mode) {
                    case ZxSDK.FETCH_ONLY ->
                            saveSplash(adInfo.getMapPid(), saAllianceAd, container);
                    case ZxSDK.FETCH_AND_SHOW ->
                            onShowSplash(adInfo, container, saAllianceAd, listener);
                }
                listener.onADLoaded(YouTui.this, -1, adInfo);
            }

            @Override
            public void onError(int code, String msg) {
                switch (mode) {
                    case ZxSDK.FETCH_ONLY ->
                            listener.onPreLoadNoAD(YouTui.this, new ZxError(String.valueOf(code), msg), adInfo);
                    case ZxSDK.FETCH_AND_SHOW ->
                            listener.onNoAD(YouTui.this, new ZxError(String.valueOf(code), msg), adInfo);
                }
            }

            @Override
            public void onResourceLoad() {

            }
        });
    }

    private static int getEcpm(BaseAllianceAd ad) {
        try {
            return Integer.parseInt(ad.getECPM());
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public boolean onShowSplash(AdInfo adInfo, ViewGroup container, @Nullable SAAllianceAd splash, BaseZxSplashListener listener) {
        if (splash != null) {
            splash.showSplash(container);
            return true;
        }
        return false;
    }

    @Override
    public void onLoadReward(Activity context, AdInfo adInfo, String mode, String type, BaseZxRewardListener listener) {
        cpmCache.put(ZxSDK.REWARD + adInfo.getMapPid(), -1);
        SAAllianceAdParams params = new SAAllianceAdParams();
        params.setPosId(adInfo.getMapPid());
        SAAllianceAd saAllianceAd = SAAllianceAdSdk.getSAAllianceAdManager().createSAAllianceAd(context);
        saAllianceAd.loadSARewardAd(params, new SARewardVideoAdLoadListener() {
            @Override
            public void onRewardVideoAdLoad(SARewardVideoAd ad) {
                if (ad == null) {
                    return;
                }
                cpmCache.put(ZxSDK.REWARD + adInfo.getMapPid(), getEcpm(ad));
                ad.setRewardVideoAdInteractionListener(new SARewardVideoAdInteractionListener() {
                    @Override
                    public void onAdShow() {
                        listener.onADExpose(YouTui.this, adInfo);
                    }

                    @Override
                    public void onAdClose() {
                        listener.onADClose(YouTui.this, adInfo);
                    }

                    @Override
                    public void onVideoComplete() {
                        listener.onVideoComplete(YouTui.this, adInfo);
                    }

                    @Override
                    public void onVideoError() {
                        listener.onVideoPlayError(YouTui.this, new ZxError("-1", "onVideoError"), adInfo);
                    }

                    @Override
                    public void onAdClick() {
                        listener.onADClick(YouTui.this, adInfo);
                    }

                    @Override
                    public void onRewardVerify() {
                        listener.onReward(YouTui.this, new HashMap<>(), adInfo);
                    }
                });
                switch (mode) {
                    case ZxSDK.FETCH_ONLY -> saveReward(adInfo.getMapPid(), saAllianceAd);
                    case ZxSDK.FETCH_AND_SHOW -> showReward(context, adInfo, listener);
                }
                listener.onADLoad(YouTui.this, adInfo);
            }

            @Override
            public void onError(int code, String message) {
                switch (mode) {
                    case ZxSDK.FETCH_ONLY ->
                            listener.onPreLoadADError(YouTui.this, new ZxError(String.valueOf(code), message), adInfo);
                    case ZxSDK.FETCH_AND_SHOW ->
                            listener.onNoAD(YouTui.this, new ZxError(String.valueOf(code), message), adInfo);
                }
            }

            @Override
            public void onResourceLoad() {

            }
        });
    }

    @Override
    public boolean onShowReward(Activity context, AdInfo adInfo, @Nullable SAAllianceAd reward, BaseZxRewardListener listener) {
        if (reward != null) {
            reward.showRewardAd(context);
            return true;
        }
        return false;
    }

    @Override
    public void onLoadInterstitial(Activity context, AdInfo adInfo, String mode, String type, BaseZxRewardListener listener) {
        cpmCache.put(ZxSDK.INTERSTITIAL + adInfo.getMapPid(), -1);
        SAAllianceAdParams params = new SAAllianceAdParams();
        params.setPosId(adInfo.getMapPid());
        params.setVideoMuted(true);
        SAAllianceAd saAllianceAd = SAAllianceAdSdk.getSAAllianceAdManager().createSAAllianceAd(context);
        saAllianceAd.loadSAInterstitialAd(params, new SAInterstitialAdLoadListener() {
            @Override
            public void onInterstitialAdLoad(SAInterstitialAd ad) {
                if (ad == null) {
                    return;
                }
                cpmCache.put(ZxSDK.INTERSTITIAL + adInfo.getMapPid(), getEcpm(ad));
                ad.setInterstitialAdInteractionListener(new SAInterstitialAdInteractionListener() {
                    @Override
                    public void onAdShow() {
                        listener.onADExpose(YouTui.this, adInfo);
                    }

                    @Override
                    public void onAdClick() {
                        listener.onADClick(YouTui.this, adInfo);
                    }

                    @Override
                    public void onAdDismiss() {
                        listener.onADClose(YouTui.this, adInfo);
                    }

                    @Override
                    public void onAdError(int code, String message) {
                        switch (mode) {
                            case ZxSDK.FETCH_ONLY ->
                                    listener.onPreLoadADError(YouTui.this, new ZxError(String.valueOf(code), message), adInfo);
                            case ZxSDK.FETCH_AND_SHOW ->
                                    listener.onNoAD(YouTui.this, new ZxError(String.valueOf(code), message), adInfo);
                        }
                    }

                    @Override
                    public void onSkippedVideo() {
                        listener.onADClose(YouTui.this, adInfo);
                    }
                });
                listener.onADLoad(YouTui.this, adInfo);
                switch (mode) {
                    case ZxSDK.FETCH_ONLY -> saveInterstitial(adInfo.getMapPid(), saAllianceAd);
                    case ZxSDK.FETCH_AND_SHOW -> showInterstitial(context, adInfo, listener);
                }

            }

            @Override
            public void onError(int code, String msg) {
                switch (mode) {
                    case ZxSDK.FETCH_ONLY ->
                            listener.onPreLoadADError(YouTui.this, new ZxError(String.valueOf(code), msg), adInfo);
                    case ZxSDK.FETCH_AND_SHOW ->
                            listener.onNoAD(YouTui.this, new ZxError(String.valueOf(code), msg), adInfo);
                }
            }

            @Override
            public void onResourceLoad() {

            }
        });
    }

    @Override
    public boolean onShowInterstitial(Activity context, AdInfo adInfo, @Nullable SAAllianceAd interstitial, BaseZxRewardListener listener) {
        if (interstitial != null) {
            interstitial.showInterstitial(context);
            return true;
        }
        return false;
    }

    @Override
    public int getCpmByPid(String type, String pid) {
        try {
            return cpmCache.get(type + pid);
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public boolean isPreResReady(String type, String pid) {
        return switch (type) {
            case ZxSDK.SPLASH -> splashMap.get(pid) != null;
            case ZxSDK.REWARD -> rewardMap.get(pid) != null;
            case ZxSDK.INTERSTITIAL -> interstitialMap.get(pid) != null;
            default -> false;
        };
    }

    @Override
    public void notifyBiddingWin(String type, String mapPid, @Nullable ReadyAdPosition loser) throws Exception {
        SAAllianceAd ad = getIBidding(type, mapPid);
        if (ad != null) {
            ad.reportBiddingSucceed(true);
        }
    }

    private SAAllianceAd getIBidding(String type, String pid) {
        SAAllianceAd iBidding = null;
        if (ZxSDK.REWARD.equals(type)) {
            iBidding = rewardMap.get(pid);
        } else if (ZxSDK.SPLASH.equals(type)) {
            iBidding = splashMap.get(pid);
        } else if (ZxSDK.INTERSTITIAL.equals(type)) {
            iBidding = interstitialMap.get(pid);
        }
        return iBidding;
    }

    @Override
    public void notifyBiddingLose(String type, String mapPid, ReadyAdPosition winner, ZxSDK.LoseReason reason) throws Exception {

    }

    @Override
    public void destroySplash(Activity activity) {

    }

    @Override
    public void destroyShownSplash(Activity activity, SAAllianceAd splashAd) {

    }

    @Override
    public void destroyReward(Activity activity) {
    }

    @Override
    public void destroyShownReward(Activity activity, SAAllianceAd rewardAd) {

    }
}
